#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QImageReader>
#include <qpixelformat.h>

#define WINDOW_TITLE    "ImageQt"
#define WINDOW_CRITICAL "Error - ImageQt"
#define WINDOW_WARNING  "Notice - ImageQt"
#define WINDOW_ABOUT    "About - ImageQt"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    leftScene=new QGraphicsScene;



    leftScene->setBackgroundBrush(QColor::fromRgb(224,224,224));

    ui->leftGraphicsView->setScene(leftScene);

    //可拖拽
    ui->leftGraphicsView->setDragMode(QGraphicsView::ScrollHandDrag);
    ui->leftGraphicsView->setMouseTracking(true);





    lblRgb=new QLabel;
    //加在状态栏左边
    ui->statusBar->addWidget(lblRgb);


    lblSize=new QLabel;
    //加在状态
   ui->statusBar->addPermanentWidget( lblSize);





    //设置label无边框
    ui->statusBar->setStyleSheet(QString("QStatusBar::item{border: 0px}"));

    //文件信息
    info=NULL;


    //鼠标移动信号
    connect(ui->leftGraphicsView ,&GraphicsView::myMouseMoveEvent,this,&MainWindow::on_graphics_myMouseMoveEvent);

}

MainWindow::~MainWindow()
{
    delete ui;

    if(lblSize)
    {
        delete  lblSize;

        lblSize=NULL;
    }

    if(lblRgb)
    {
        delete lblRgb;
        lblRgb=NULL;
    }

    if(leftScene)
    {
        delete leftScene;

        leftScene=NULL;
    }
}


void MainWindow::cleanImage()
{

    //清空图像信息
    pixImageWidth=0;
    pixImageHeight=0;

    if(!qImage.isNull())
    {
          qImage.~QImage();

    }



    leftScene->clear();
    ui->leftGraphicsView->resetTransform();


//    if (lblSize)
//    {
//        delete lblSize;
//        lblSize = new QLabel;
//        ui->statusBar->addPermanentWidget(lblSize);
//    }


    lblRgb->setText("");
    lblSize->setText("");



    this->setWindowTitle(WINDOW_TITLE);
}

void MainWindow::showImage(int argc, char *argv[])
{
    //两个参数
    if(argc==2)
    {
        //图片文件
          QString imagePath(argv[1]);
          OpenImage(imagePath);
    }


}




void MainWindow::on_actionOpen_triggered()
{

    // Automatically detects the current user's home directory
    // And then wait the user to select one image
    QString imagePath = QFileDialog::getOpenFileName(this, tr("Open image"), getUserPath() + "/Pictures",
                                             tr("All Files (*);;"
                                                "All Images (*.bmp *.gif *.jpg *.jpeg *.png *.ppm *.xbm *.xpm);;"
                                                "Image bmp (*.bmp);;"
                                                "Image GIF (*.gif);;"
                                                "Image JPG (*.jpg);;"
                                                "Image JPEG (*.jpeg);;"
                                                "Image PNG (*.png);;"
                                                "Image PPM (*.ppm);;"
                                                "Image XBM (*.xbm);;"
                                                "Image XPM (*.xpm);;"));

   OpenImage(imagePath);


}


void MainWindow::OpenImage(QString imagePath)
{
    if(!imagePath.isEmpty())
    {
        QFile file(imagePath);

        if(!file.open(QIODevice::ReadOnly))
        {
            QMessageBox::critical(this,tr(WINDOW_CRITICAL),tr("Unable to open image"));
            return;
        }


        //delete previous image
        cleanImage();

        //upload image
        info=new QFileInfo(imagePath);



      QPixmap leftPixmap(imagePath);


        leftPixmapItem=leftScene->addPixmap(leftPixmap);



        leftScene->setSceneRect(QRectF(leftPixmap.rect()));




        //settings
        this->setWindowTitle(info->fileName()+" - ImageQt");

        lblSize->setText(QString::number(leftPixmapItem->pixmap().width())
                      + " x " + QString::number(leftPixmapItem->pixmap().height()));



        //记录打开图像的 宽 高
        pixImageWidth=leftPixmapItem->pixmap().width();
        pixImageHeight=leftPixmapItem->pixmap().height();

      qImage=leftPixmapItem->pixmap().toImage();

    }
}

/******************************************************************************
 *                           获得当前用户的用户名
 *****************************************************************************/
QString MainWindow::getUserName()
{
    QString userPath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
    QString userName = userPath.section("/", -1, -1);
    return userName;
}

/******************************************************************************
 *                              获得当前用户的家目录
 *****************************************************************************/
QString MainWindow::getUserPath()
{
    QString userPath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
    return userPath;
}

void MainWindow::on_actionClose_triggered()
{

    cleanImage();
}

void MainWindow::on_actionExit_triggered()
{

    qApp->quit();
}


//获取鼠标点的坐标 2019-05-07 ludc
void MainWindow::on_graphics_myMouseMoveEvent(QMouseEvent *e)
{
    //存在图像了
    if(pixImageWidth>0&&pixImageHeight>0)
    {
        //获取鼠标点的坐标
        QPointF p=  ui->leftGraphicsView->mapToScene(e->pos());
        //
        pix_X=p.rx();
        pix_Y=p.ry();

        //图像区域内
        if(pix_X>=0&&pix_X<=pixImageWidth&&pix_Y>=0&&pix_Y<=pixImageHeight)
        {
            if(!qImage.isNull())
            {
               pixColor=  qImage.pixelColor(pix_X,pix_Y);


//               lblSize->setText("wh:("+QString::number(pixImageWidth)+","+QString::number(pixImageHeight)+") xy:(" + QString::number(pix_X) + "," +QString::number(pix_Y)+") ; rgb:("+QString::number(pixColor.red())+","+QString::number(pixColor.green())+","+QString::number(pixColor.blue())+")" );

lblRgb  ->setText("rgb:("+QString::number(pixColor.red())+","+QString::number(pixColor.green())+","+QString::number(pixColor.blue())+") xy:(" + QString::number(pix_X) + "," +QString::number(pix_Y)+")" );

            }



        }

    }
}

